package com.py.credit.service.impl;

import com.py.credit.common.LhjcUserCacheHelper;
import com.py.credit.dao.GzDao;
import com.py.credit.dao.SubjectCenterDao;
import com.py.credit.entity.SysUser;
import com.py.credit.service.LhjcAjbhService;
import com.py.credit.service.SubjectCenterInterface;
import com.py.credit.utils.UuidUtil;
import com.py.credit.utils.XmlUtil;
import com.py.framework.core.utils.SerializingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SubjectCenterInterfaceImpl implements SubjectCenterInterface {

    @Autowired
    private SubjectCenterDao subjectCenterDao;

    @Autowired
    private LhjcAjbhService lhjcAjbhService;

    @Autowired
    private GzDao gzDao;

    private static List<String> ysajhList;

    @Override
    public Map<String, Object> getFeedbackData(Map<String, List<String>> result, Map<String, List<Object>> credit, long deptId, String username, long userId, SysUser user, String token) {
        //当前时间，如果处罚是5年
        Map<String, Object> data = new HashMap<String, Object>();
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.YEAR, 5);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        String qyid = result.get("id") == null ? "" : result.get("id").get(0);
        String qymc = result.get("qymc") == null ? "" : result.get("qymc").get(0);
        String tyshxydm = result.get("tyshxydm") == null ? "" : result.get("tyshxydm").get(0);
        String zjgdm = result.get("zjgdm") == null ? "" : result.get("zjgdm").get(0);
        String gsdjm = result.get("gsdjm") == null ? "" : result.get("gsdjm").get(0);
        String swdjh = result.get("swdjh") == null ? "" : result.get("swdjh").get(0);
        String hongmdsl = result.get("hongmdsl") == null ? "0" : result.get("hongmdsl").get(0);
        String heimdsl = result.get("heimdsl") == null ? "0" : result.get("heimdsl").get(0);

        Map<String, String> frInfo = subjectCenterDao.selectFrInfo(qymc);
        String uuid = UuidUtil.getInstance().getUUid();
        boolean addHongHeimdFlag = null == frInfo ? true : false;
        String tybm = "";

        if (addHongHeimdFlag) {
            subjectCenterDao.insertFrInfo(uuid, qymc, tyshxydm, zjgdm, gsdjm, swdjh, Integer.parseInt(hongmdsl), Integer.parseInt(heimdsl));
            tybm = uuid;
        } else {
            tybm = frInfo.get("tybm");
        }

        ysajhList = new ArrayList<String>();
        List<String> heimdList = this.getMdList(credit, XmlUtil.HEIMD_KEY, XmlUtil.HEIMD_TYPE, uuid, addHongHeimdFlag);
        List<String> hongmdList = this.getMdList(credit, XmlUtil.HONGMD_KEY, XmlUtil.HONGMD_TYPE, uuid, addHongHeimdFlag);

        List<Map<String, Object>> heimdGzList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> heimdCsList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> hongmdGzList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> hongmdCsList = new ArrayList<Map<String, Object>>();

        if (null != heimdList && heimdList.size() > 0) {
            heimdGzList = subjectCenterDao.selectGzDetail(heimdList);
            heimdCsList = subjectCenterDao.selectCsDetail(deptId, heimdList);

            for (int i = 0; i < heimdGzList.size(); i++) {
                //判断机构信息是否还在处罚中
                for (String ysajh : ysajhList
                        ) {
                    Date jzrqDate = lhjcAjbhService.selectJzrqDate("C", qymc, "", (String) heimdGzList.get(i).get("bm"), ysajh);
                    boolean flag = null != jzrqDate && (now.getTime() <= jzrqDate.getTime() || sdf.format(now).equals(sdf.format(jzrqDate)));
                    if (!flag) {
                        lhjcAjbhService.addFr(gzDao.selectById(heimdGzList.get(i).get("id") + ""), "C", tybm, qymc, tyshxydm, zjgdm, gsdjm,
                                sdf.format(calendar.getTime()), now, userId, deptId, ysajh, token, user);
                    }
                }
            }

            for (Map<String, Object> cs : heimdCsList
                    ) {
                cs.put("ajbh", cs.get("id") + "-" + cs.get("remark") + "-" + subjectCenterDao.selectAjbhByCsIdAndQymc((String) cs.get("id"), qymc));
            }
        }

        if (null != hongmdList && hongmdList.size() > 0) {
            hongmdGzList = subjectCenterDao.selectGzDetail(hongmdList);
            hongmdCsList = subjectCenterDao.selectCsDetail(deptId, hongmdList);

            for (int i = 0; i < hongmdGzList.size(); i++) {
                //判断机构信息是否还在激励中
                Date jzrqDate = lhjcAjbhService.selectJzrqDate("C", qymc, "", (String) hongmdGzList.get(i).get("bm"), "");
                boolean flag = null != jzrqDate && (now.getTime() <= jzrqDate.getTime() || sdf.format(now).equals(sdf.format(jzrqDate)));
                if (!flag) {
                    lhjcAjbhService.addFr(gzDao.selectById(heimdGzList.get(i).get("id") + ""), "J", tybm, qymc, tyshxydm, zjgdm, gsdjm,
                            sdf.format(calendar.getTime()), now, userId, deptId, "", token, user);
                }
            }

            for (Map<String, Object> cs : hongmdCsList
                    ) {
                cs.put("ajbh", cs.get("id") + "-" + cs.get("remark") + "-" + subjectCenterDao.selectAjbhByCsIdAndQymc((String) cs.get("id"), qymc));
            }
        }

        data.put("qyid", qyid);
        data.put("qymc", qymc);
        data.put("tyshxydm", tyshxydm);
        data.put("zjgdm", zjgdm);
        data.put("gsdjm", gsdjm);
        data.put("swdjh", swdjh);
        data.put("username", username);
        data.put("heimdGzList", heimdGzList);
        data.put("hongmdGzList", hongmdGzList);
        data.put("heimdCsList", heimdCsList);
        data.put("hongmdCsList", hongmdCsList);
        return data;
    }

    @Override
    public long getCfbmByAjbh(String ajbh) {
        return subjectCenterDao.selectCfbmByAjbh(ajbh);
    }

    @Override
    public void putUserSessionData(String jdToken, SysUser user) {
        // 缓存用户信息
        LhjcUserCacheHelper.putUserCache(SerializingUtil.serialize(jdToken), SerializingUtil.serialize(user));
    }

    @Override
    public void clearUserSessionData(String jdToken) {
        // 清空用户信息
        LhjcUserCacheHelper.clearUserCache(SerializingUtil.serialize(jdToken));
    }

    private List<String> getMdList(Map<String, List<Object>> credit, String key, String type, String uuid, boolean flag) {
        List<String> mdList = new ArrayList<String>();
        List<Object> md = (List<Object>) credit.get(key);
        if (null != md) {
            for (int i = 0; i < md.size(); i++) {
                Map<String, List<Object>> unit = (Map<String, List<Object>>) md.get(i);
                Set<Map.Entry<String, List<Object>>> set = unit.entrySet();
                for (Map.Entry<String, List<Object>> e : set) {
                    List<Object> tableList = (List<Object>) e.getValue();
                    mdList = this.getDataList(tableList, mdList, type, uuid, flag);
                }
            }
        }

        return mdList;
    }

    private List<String> getDataList(List<Object> tableList, List<String> mdList, String type, String uuid, boolean flag) {
        if (null != tableList) {
            for (Object table : tableList
                    ) {
                Map<String, List<Object>> md = (Map<String, List<Object>>) table;
                Set<Map.Entry<String, List<Object>>> set = md.entrySet();
                for (Map.Entry<String, List<Object>> e : set) {
                    if (e.getKey().endsWith(type)) {
                        mdList.add(e.getKey().split("-")[1]);

                        if (type.equals(XmlUtil.HEIMD_TYPE)) {
                            this.addYsajhList(e.getValue());
                        }

                        if (flag) {
                            this.addHongHeimd(e.getValue(), type, uuid);
                        }
                    }
                }
            }
        }

        return mdList;
    }

    private void addHongHeimd(List<Object> list, String type, String tybm) {
        for (Object object : list
                ) {
            Map<String, Object> map = (Map<String, Object>) object;
            List<Map<String, List<String>>> rowList = (List<Map<String, List<String>>>) map.get("row");
            for (Map<String, List<String>> row : rowList
                    ) {
                if (type.equals(XmlUtil.HEIMD_TYPE)) {
                    subjectCenterDao.insertHeimd(tybm, row.get("item-地区名字").get(0), row.get("item").get(0),
                            row.get("item-履行情况").get(0), row.get("item-案号").get(0), row.get("item-生效法律文书确定的义务").get(0),
                            row.get("item-登记时间").get(0), row.get("item-执行依据文号").get(0), row.get("item-被执行人姓名/名称").get(0),
                            row.get("item-发布时间").get(0), row.get("item-做出执行依据单位").get(0), row.get("item-失信被执行人行为具体情形").get(0),
                            row.get("item-执行法院").get(0), row.get("item-证件号/组织机构代码").get(0));
                } else {
                    subjectCenterDao.insertHongmd(tybm, row.get("item-统一社会信用代码").get(0), row.get("item").get(0),
                            row.get("item-评价年度").get(0), row.get("item-纳税人识别号").get(0), row.get("item-纳税人名称").get(0),
                            row.get("item-发布时间").get(0), row.get("item-主管税务机关").get(0));
                }
            }
        }
    }

    private void addYsajhList(List<Object> list) {
        for (Object object : list
                ) {
            Map<String, Object> map = (Map<String, Object>) object;
            List<Map<String, List<String>>> rowList = (List<Map<String, List<String>>>) map.get("row");
            for (Map<String, List<String>> row : rowList
                    ) {
                ysajhList.add(row.get("item-案号").get(0));
            }
        }
    }
}
